package com.ma.netana.utils;

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.packet.Packet;
import jpcap.packet.UDPPacket;

import java.io.IOException;

public class UDPAna {
    public UDPPacket udpAna() {

        //UDPPacket res_udPacket = new UDPPacket(18020,8080);
        Packet packet_test = new Packet();
        /*--------------	第一步绑定网络设备       --------------*/
        NetworkInterface[] devices = JpcapCaptor.getDeviceList();

        for(NetworkInterface n : devices)
        {
            System.out.println(n.name + "     |     " + n.description);
        }
        System.out.println("-------------------------------------------");
        JpcapCaptor jpcap = null;
        int caplen = 1512;
        boolean promiscCheck = true;
        try{
            jpcap = JpcapCaptor.openDevice(devices[1], caplen, promiscCheck, 50);
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        /*----------第二步抓包-----------------*/
        int i = 0;
        while(i < 2)
        {
            Packet packet  = jpcap.getPacket();
            packet_test = packet;
            //res_udPacket =(UDPPacket) packet;
//            UDPPacket udpPacket = (UDPPacket) packet;//强转
//            System.out.println("packet"+" "+packet);
//            System.out.println("udpPacket"+" "+udpPacket);


            if(packet instanceof UDPPacket)
            {
                /*
                存在问题：将捕获到的数据包按16进制或二进制打印到控制台
                */
                byte [] bytes = packet.data;
                int n = 0;
                for (byte aByte : bytes) {
                    Integer.toHexString(aByte);
                    if (aByte>=0) {
                        System.out.print(Integer.toBinaryString((byte)aByte) + ' ');
                    }else {
                        aByte = (byte)-aByte;
                        System.out.print('-'+Integer.toBinaryString((byte)aByte) + ' ');
                    }
                    n++;
                    if (n == 10){
                        System.out.println();
                        n = 0;
                    }
                }
                System.out.println();
                i++;

                UDPPacket udpPacket1_1 = (UDPPacket) packet;//强转

                System.out.println("udpPro:"+""+udpPacket1_1.protocol);
                System.out.println("udpFlag:"+""+udpPacket1_1.r_flag);
                System.out.println("udpLen:"+""+udpPacket1_1.len);
                System.out.println("udpSrc:"+""+udpPacket1_1.src_ip);
                System.out.println("udpDes:"+""+udpPacket1_1.dst_ip);

//                System.out.println();
//                System.out.println("版本：IPv4");
//                System.out.println("优先权：" + ip.priority);
//                System.out.println("区分服务：最大的吞吐量： " + ip.t_flag);
//                System.out.println("区分服务：最高的可靠性：" + ip.r_flag);
//                System.out.println("长度：" + ip.length);
//                System.out.println("标识：" + ip.ident);
//                System.out.println("DF:Don't Fragment: " + ip.dont_frag);
//                System.out.println("NF:Nore Fragment: " + ip.more_frag);
//                System.out.println("片偏移：" + ip.offset);
//                System.out.println("生存时间："+ ip.hop_limit);
//                String protocol ="";
//                switch(new Integer(ip.protocol))
//                {
//                    case 1:protocol = "ICMP";break;
//                    case 2:protocol = "IGMP";break;
//                    case 6:protocol = "TCP";break;
//                    case 8:protocol = "EGP";break;
//                    case 9:protocol = "IGP";break;
//                    case 17:protocol = "UDP";break;
//                    case 41:protocol = "IPv6";break;
//                    case 89:protocol = "OSPF";break;
//                    default : break;
//                }
//                System.out.println("协议：" + protocol);
//                System.out.println("源IP " + ip.src_ip.getHostAddress());
//                System.out.println("目的IP " + ip.dst_ip.getHostAddress());
//                System.out.println("源主机名： " + ip.src_ip);
//                System.out.println("目的主机名： " + ip.dst_ip);
//                System.out.println("----------------------------------------------");
            }

        }
        return (UDPPacket)packet_test;
    }


}
